clear all, close all, clc;

init_name;


%%
% if ~IF_SERVER
%     data_base = data_base(1:2);
% end



for i = 1:length(data_base)
    data_base{i}.frame_n = (data_base{i}.end - data_base{i}.start)...
                            /data_base{i}.rate+1;
end

classifier_n = 0;

for i = 1:length(data_base)
    try
        fname = [score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_score_record.bin' ];
        data_base{i}.score_matrix = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.score_matrix = ones(1,1);
    end
    
    try
        fname = [ score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_corr_pos.bin' ];
        data_base{i}.corr_pos = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.corr_pos = ones(1,1);
    end
    
    try
        fname = [ score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_corr_neg.bin' ];
        data_base{i}.corr_neg = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.corr_neg = ones(1,1);
    end
            
    
    if(sum(size( data_base{i}.score_matrix ) ) == 2 ...
        || ((i~=1) && (size(data_base{i}.score_matrix,2) ~= classifier_n * 5) ) )
    
        if i==1
            disp('score_matrix unfound on training data!')
        else
            disp('score_matrix unfound or unmatch on testing!')
            data_base{i}.score_matrix = rand(    data_base{i}.frame_n, ...
                                    classifier_n*5 );
        end
    end
    
    if(data_base{i}.frame_n < size( data_base{i}.score_matrix ,1 ) )
        data_base{i}.score_matrix = ...
            data_base{i}.score_matrix(1:data_base{i}.frame_n,:);
    end
    
    if i==1
        classifier_n = size(data_base{i}.score_matrix,2) / 5;        
        disp( [num2str( classifier_n ) ' classifiers in the pool'] )
    end
    
    if( size( data_base{i}.corr_pos ,1) ~= classifier_n )
        disp(['corr_pos in database ' num2str(i) ' unfound or unmatch']);
        data_base{i}.corr_pos = eye( classifier_n , classifier_n );
    end
    
    
    
    if( size( data_base{i}.corr_neg ,1) ~= classifier_n )
        disp(['corr_neg in database ' num2str(i) ' unfound or unmatch']);
        data_base{i}.corr_neg = eye( classifier_n , classifier_n );
    end        
        
end

%%


for i = 1:length(data_base)
    for j = 1: global_n
        fname = [score_root data_base{i}.name '_global_feat_' ...
                 num2str( j - 1 ) '.bin' ];
        temp = getMat( fname )';               
        
        temp = temp/255;
        
        data_base{i}.global_feat{j} = temp(1:data_base{i}.frame_n,:);
    end
end